package com.deer.wms.common.config;

import org.junit.Test;
import redis.clients.jedis.*;
import redis.clients.jedis.util.ShardInfo;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class TestJedis {
    @Test
    public void test01() {
        JedisPoolConfig jedisConfig = new JedisPoolConfig();
        jedisConfig.setMaxIdle(20);
        jedisConfig.setTestOnBorrow(false);
        List<JedisShardInfo> shards = new ArrayList<>();
        shards.add(new JedisShardInfo("192.168.1.250","6379"));
        shards.add(new JedisShardInfo("192.168.1.251","6380"));
        shards.add(new JedisShardInfo("192.168.1.252","6381"));
        ShardedJedisPool shardedJedisPool = new ShardedJedisPool(jedisConfig, shards);
        ShardedJedis ShardedJedis = null;
        try { ShardedJedis = shardedJedisPool.getResource();
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{ ShardedJedis.close(); shardedJedisPool.close(); }
        ShardedJedis.set("1", "redis分片测试成功!!!");
        String aaa = ShardedJedis.get("1");
        System.out.println(aaa);
    }
    //redis哨兵测试
    @Test
    public void test02(){
        //定义哨兵的集合信息       host:port
        Set<String> sentinels = new HashSet<String>();
        sentinels.add("192.168.1.250:26379");
        sentinels.add("192.168.1.250:26380");
        sentinels.add("192.168.1.250:26381");
        JedisSentinelPool pool = new JedisSentinelPool("mymaster", sentinels);
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            jedis.set("hello", "world");
            String value = jedis.get("hello");
            System.out.println(value);
        } catch (Exception e) {
            System.out.println(e);
        }
        finally {
            if (jedis != null)
                try { jedis.close();
                    pool.close();
                } catch (Exception e) {
                    e.printStackTrace(); }
        }
    }
}
